import random
import datetime

money = 2000
initial_money = money
# 购物车
gouwuche = []
shangpin = {'苹果': 500, '香蕉': 600, '菠萝': 400, '葡萄': 2000, '空调': 1800}
youhuiquan = [
    {'name': '空调8折券', 'item': '空调', 'discount': 0.8},
    {'name': '苹果9折券', 'item': '苹果', 'discount': 0.9},
    {'name': '香蕉9.5折券', 'item': '香蕉', 'discount': 0.95},
    {'name': '菠萝8.5折券', 'item': '菠萝', 'discount': 0.85},
    {'name': '葡萄5折券', 'item': '菠萝', 'discount': 0.5},
]
kaishishijian = datetime.datetime.now()
myyouhuiquan = random.choice(youhuiquan)
print('欢迎进入超市,请购买:')
print(f"恭喜您,进场时抽中了一张【{myyouhuiquan['name']}】!")
print(shangpin)
if myyouhuiquan['item']:
    print(f"购买{myyouhuiquan['item']}时可享受优惠.")

while True:
    print(
        f"您当前余额: {money:.2f}元\n"
        "请输入想购买的商品名称\n"
        "输入 '退货' 来退掉购物车中的商品\n"
        "输入 'q' 或 'Q' 结账退出\n"
        "您的选择是: "
    )
    name = str(input())
    if name == 'q' or name == 'Q':
        print("感谢您的光临，正在为您结算...")
        break

    elif name == '退货':
        if not gouwuche:
            print("您的购物车是空的，无法退货！")
            continue
        print(f"您当前的购物车中有: {', '.join(gouwuche)}")
        tuihuo = input("请输入您想退货的商品名称: ")

        if tuihuo in gouwuche:
            tuihuo_price = shangpin[tuihuo]
            if myyouhuiquan['item'] == tuihuo_price:
                tuihuo_price *= myyouhuiquan['discount']
                print(f"（此商品享受了【{myyouhuiquan['name']}】，按折扣价退款）")
            money += tuihuo_price
            gouwuche.remove(tuihuo)
            print(f"商品 '{tuihuo}' 退货成功! 退款{tuihuo_price:.2f}元, 您的余额: {money:.2f}元")
            print(f"当前购物车: {gouwuche}")
        else:
            print(f"您的购物车中没有 '{tuihuo}' 这件商品！")
        continue

    elif name in shangpin and money >= shangpin[name]:
        price = shangpin[name]
        if myyouhuiquan['item'] == name:
            price *= myyouhuiquan['discount']
            print(f"已为您使用【{myyouhuiquan['name']}】，商品 '{name}' 折后价为: {price:.2f}元")
        if money >= price:
            money -= price
            gouwuche.append(name)
            print('恭喜购买成功,卡里余额:', money)
            print(f"当前购物车: {gouwuche}")

            name = str(input(f'你还可以购买:{shangpin},\n输入你想要的商品,退货或者输入 Q/q 退出:'))
            if name == 'q' or name == 'Q':
                break
            elif name == '退货':
                if not gouwuche:
                    print("您的购物车是空的，无法退货！")
                continue
            print(f"您当前的购物车中有: {', '.join(gouwuche)}")
            tuihuo = input("请输入您想退货的商品名称: ")
        elif name in shangpin and money >= shangpin[name]:
            price = shangpin[name]
    elif money < shangpin[name]:
        print('穷鬼，钱不够！请到其他超市购买！')
        break
    elif name not in shangpin:
        print("没有这个商品,请重新输入:")

total_items = len(gouwuche)
total_spent = initial_money - money

print(f"购买商品总数: {total_items} 件")
print(f"商品总消费: {total_spent:.2f} 元")
print(f"卡上余额: {money:.2f} 元")
